<?php

header("Content-type: text/html; charset=utf-8");

$db_host = "localhost";    //数据库服务器名称
$db_user = "root";             // 连接数据库用户名
$db_psw = "";                // 连接数据库密码
$db_name = "my_db";           // 数据库名称

$sql = "CREATE TABLE IF NOT EXISTS Persons
(
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
FirstName varchar(15),
LastName varchar(15),
Age int
)";

$sql = "SELECT * FROM Persons";
$dsn = "mysql:host=$db_host;dbname=$db_name";
try{
    $db = new PDO($dsn, $db_user, $db_psw);
} catch (PDOException $e) {
    print "Error: " . $e->getMessage() . "<br/>";
    die();
}

foreach($db->query($sql) as $row ){
    print_r($row) ;
    print "<br>";
}

print "<hr>";
$rs = $db->query($sql);
$rs->setFetchMode(PDO::FETCH_ASSOC);
while($row = $rs->fetch()){
    print_r($row);
    print "<br>";
}

print "<hr>";
$rs = $db->query("SELECT COUNT(*) FROM Persons");
$col = $rs->fetchColumn();
echo "col: " , $col;



// close
$db = null;





